import { Component, Input, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { map } from 'rxjs/operators';
import { TicketService } from '../../../services/ticket.service';

@Component({
  selector: 'app-print-order-modal',
  templateUrl: './print-order-modal.component.html',
  styleUrls: ['./print-order-modal.component.less'],
  encapsulation: ViewEncapsulation.None
})
export class PrintOrderModalComponent implements OnInit {
  data: any = {};

  type = 1;

  rcontactInfos: any[] = [];
  scontactInfos: any[] = [];

  @Input()
  vatappcodes: string[] = [];

  constructor(private modal: NzModalRef, public msgSrv: NzMessageService, public service: TicketService) {}

  ngOnInit(): void {
    this.loadAddress();
  }

  loadAddress() {
    this.service.request(this.service.$api_get_order_summary_path, this.vatappcodes).subscribe(res => {
      if (res) {
        this.rcontactInfos = res.rcontactInfos || [];
        this.scontactInfos = res.scontactInfos || [];
      }
    });
  }

  sure() {
    if (this.type === 1) {
      this.service.request(this.service.$api_create_express + '?_allow_badcode=true', this.vatappcodes).subscribe(res => {
        if (res.status === 200) {
          if (res?.data?.length > 0) {
            this.getPDF(res.data);
          } else {
            this.service.msgSrv.success('快递下单成功，请到快递信息页面打印');
            this.modal.destroy(true);
          }
        }
      });
    } else {
      if (!this.data.rcontactInfo || !this.data.scontactInfo) {
        this.service.msgSrv.warning('请选择收件地址和寄件地');
        return;
      }
      const params = {
        rcontactInfo: this.data.rcontactInfo,
        ltdId: this.data.scontactInfo.ltdId,
        shipperId: this.data.scontactInfo.shipperId,
        vatappcodes: this.vatappcodes,
        scontactInfo: this.data.scontactInfo
      };
      delete this.data.scontactInfo.ltdId;
      delete this.data.scontactInfo.shipperId;
      delete this.data.scontactInfo.id;
      this.service.request(this.service.$api_get_order_summary + '?_allow_badcode=true', params).subscribe(res => {
        if (res.status === 200) {
          if (res?.data?.length > 0) {
            this.service.reviewPDF(res?.data);
          } else {
            this.service.msgSrv.success('快递下单成功，请到快递信息页面打印');
            this.modal.destroy(true);
          }
        }
      });
    }
  }

  getPDF(ids: Array<string>) {
    this.service.request(this.service.$api_get_print_pdf, ids).subscribe(res => {
      if (res?.pdfUrl) {
        this.service.msgSrv.success('操作成功');
        this.modal.destroy(true);
        this.service.reviewPDF(res.pdfUrl);
      } else {
        this.service.msgSrv.success('快递下单成功，请到快递信息页面打印');
        this.modal.destroy(true);
      }
    });
  }

  close() {
    this.modal.destroy();
  }
}
